<?php

namespace app\app\controller;
use think\Db;

class Seller extends Common
{
    //获取推荐商家
    public function get_flag()
    {
        $uid = token_decode(req('token'));
        $where = get_pca();
        $cate_id = req('cate_id');
        if ($cate_id) {
            $where['cate_id'] = $cate_id;
        }

        $where['flag'] = 1;
        $where['status'] = 1;
        $lng = req('lng');
        $lat = req('lat');

        $whereIn = '';
        $whereIn_field = null;
        $active = req('active');
        $consume = req('consume');
        $attr = req('attr');
        if (!empty($active)) {
            if ($active == 'money_off') {
                $whereIn_field = 'id';
                $money_off_seller = Db::table('money_off')->group('seller_id')->select();
                foreach ($money_off_seller as $index => $item) {
                    $whereIn .= $item['seller_id'] . ',';
                }
                $whereIn = substr($whereIn, 0, strlen($whereIn) - 1);
            } elseif ($active == 'present') {
                $whereIn_field = 'id';
                $money_off_seller = Db::table('service')->where('present!=""')->group('seller_id')->select();
                foreach ($money_off_seller as $index => $item) {
                    $whereIn .= $item['seller_id'] . ',';
                }
                $whereIn = substr($whereIn, 0, strlen($whereIn) - 1);
            } elseif ($active == 'new_user_reduction') {
                $where[$active] = ['<>', ''];
            }
        }
        if (!empty($consume)) {
            $where['consume'] = explode('&', $consume);
        }
        if (!empty($attr)) {
            if ($attr == 'brand') {
                $where['label'] = '品牌';
            } elseif ($attr == 'advance') {
                $where['advance'] = 1;
            }
        }

        $fields = 'id as seller_id,aid,volume_date,headimg,name,min_express_charge,lng,lat,new_user_reduction,volume as month_sale,label,business,introduce,phone,address,advance';
		$page = req('page') ? req('page') : 1;
        $page_size = req('page_size') ? req('page_size') : 10;
        $data = Db::table('seller')->field($fields)->whereIn($whereIn_field, $whereIn)->where($where)->page($page, $page_size)->order('flag_sort desc')->select();
        foreach ($data as $k => $v) {
			$data[$k]['grade'] = Db::table('evaluate')->where('seller_id', $v['seller_id'])->where('time>complete_time or complete_time<=' . time() - 30000)->avg('star');
			$data[$k]['grade'] = $data[$k]['grade']?(string)(round($data[$k]['grade'], 1)):'5';
            if($v['volume_date'] != date('Ym')){
                Db::table('seller')->where('id',$v['seller_id'])->update(['volume'=>0,'volume_date'=>date('Ym')]);
            }
            $data[$k]['month_sale'] = (int)$v['month_sale'];
            $data[$k]['min_express_charge'] = (int)$v['min_express_charge'];
            $data[$k]['new_user_reduction'] = (string)$v['new_user_reduction'];
            $data[$k]['distance'] = (string)($this->getDistance($lat, $lng, $v['lat'], $v['lng']) / 1000);  //距离
            $data[$k]['express_time'] = '40分钟';  //配送时间
            $data[$k]['introduce'] = (string)$v['introduce'];
            $data[$k]['phone'] = (string)$v['phone'];
            $data[$k]['address'] = (string)$v['address'];

            $express_config = Db::table('express_config')->where('aid',$v['aid'])->value('charge');
            //计算配送费
			$pi = $data[$k]['distance'];
			$express_charge = $express_config['charge'];
			if($pi > $express_config['scope']){
				$express_charge += $express_config['add_charge']*($pi-$express_config['scope']);
			}
			if ($express_charge < $express_config['charge']) {
				$express_charge = $express_config['charge'];
			}
			$data[$k]['express_charge'] = (string)round($express_charge);

            unset($data[$k]['lng']);
            unset($data[$k]['lat']);
            $money_off = Db::table('money_off')->where('seller_id', $v['seller_id'])->select();
            $string = '';
            foreach ($money_off as $k2 => $v2) {
                $string .= '满' . $v2['full'] . '减' . $v2['minus'] . ';';
            }
            if (!empty($v['new_user_reduction'])) {
                $data[$k]['new_user_reduction'] = '新用户下单立减' . $v['new_user_reduction'];
            }
            $data[$k]['money_off'] = (string)$string;
            $data[$k]['isFavourite'] = (int)Db::table('favourite')->where(['uid' => $uid, 'seller_id' => $v['seller_id']])->count();
        }
        return json(['state' => 1, 'data' => $data, 'msg' => '获取成功']);
    }

    //获取附近商家
    public function get_nearby()
    {
        $uid = token_decode(req('token'));
        $lng = req('lng');
        $lat = req('lat');

        $data = [];
        $state = 0;
        if (!$lng || !$lng) {
            $msg = '无法获取定位信息';
        } else {
            $state = 1;
            $fields = 'id as seller_id,aid,volume_date,headimg,name,min_express_charge,lng,lat,new_user_reduction,volume as month_sale,label,business,introduce,phone,address,advance';

            $whereIn = '';
            $whereIn_field = null;
            $active = req('active');
            $consume = req('consume');
            $attr = req('attr');
            if (!empty($active)) {
                if ($active == 'money_off') {
                    $whereIn_field = 'id';
                    $money_off_seller = Db::table('money_off')->group('seller_id')->select();
                    foreach ($money_off_seller as $index => $item) {
                        $whereIn .= $item['seller_id'] . ',';
                    }
                    $whereIn = substr($whereIn, 0, strlen($whereIn) - 1);
                } elseif ($active == 'present') {
                    $whereIn_field = 'id';
                    $money_off_seller = Db::table('service')->where('present!=""')->group('seller_id')->select();
                    foreach ($money_off_seller as $index => $item) {
                        $whereIn .= $item['seller_id'] . ',';
                    }
                    $whereIn = substr($whereIn, 0, strlen($whereIn) - 1);
                } elseif ($active == 'new_user_reduction') {
                    $where[$active] = ['<>', ''];
                }
            }
            if (!empty($consume)) {
                $where['consume'] = explode('&', $consume);
            }
            if (!empty($attr)) {
                if ($attr == 'brand') {
                    $where['label'] = '品牌';
                } elseif ($attr == 'advance') {
                    $where['advance'] = 1;
                }
            }

            $pi = floatval($this->get_lat_lng(3));
            $page = req('page') ? req('page') : 1;
            $page_size = req('page_size') ? req('page_size') : 10;
            $where = $lat . '-' . $pi . ' and lat < ' . $lat . '+' . $pi . ' and lng > ' . $lng . '-' . $pi . ' and lng < ' . $lng . '+' . $pi;
            $cate_id = req('cate_id');
            if ($cate_id) {
                $where .= " and cate_id='$cate_id'";
            }
            $order = '(6378.138 * 2 * asin(sqrt(pow(sin((' . $lat . ' * 3.141592654 / 180 - 31.3463920 * 3.141592654 / 180) / 2),2) + cos(' . $lat . ' * 3.141592654 / 180) * cos(31.3463920 * 3.141592654 / 180) * pow(sin((lng * 3.141592654 / 180 - 121.444192 * 3.141592654 / 180) / 2),2))) * 1000) asc,business desc';
			$wherepca = get_pca();
            $data = Db::table('seller')->field($fields)->where($where)->where('status', 1)->where($wherepca)->order($order)->page($page, $page_size)->select();
			// var_dump(Db::table('seller')->getLastSql());exit;
			// $seller_list = Db::query('SELECT *,(6378.138 * 2 * asin(sqrt(pow(sin((' . $lat . ' * 3.141592654 / 180 - 31.3463920 * 3.141592654 / 180) / 2),2) + cos(' . $lat . ' * 3.141592654 / 180) * cos(31.3463920 * 3.141592654 / 180) * pow(sin((lng * 3.141592654 / 180 - 121.444192 * 3.141592654 / 180) / 2),2))) * 1000) AS distance FROM`seller` `t` WHERE `status` = 1 AND `aid` = 371725 ORDER BY distance ASC LIMIT 0,30');
			//var_dump($seller_list);exit;
            // $seller_id_string = '';
            // foreach ($seller_list as $k => $v) {
                // $seller_id_string .= $v['id'] . ',';
            // }
            // $seller_id_string = substr($seller_id_string, 0, strlen($seller_id_string) - 1);
            // $data = Db::table('seller')->field($fields)->whereIn('id', $seller_id_string)->select();
            foreach (array_reverse($data) as $k => $v) {
                $data[$k]['grade'] = Db::table('evaluate')->where('seller_id', $v['seller_id'])->where('time>complete_time or complete_time<=' . time() - 30000)->avg('star');
				$data[$k]['grade'] = $data[$k]['grade']?(string)(round($data[$k]['grade'], 1)):'5';
                if($v['volume_date'] != date('Ym')){
                    Db::table('seller')->where('id',$v['seller_id'])->update(['volume'=>0,'volume_date'=>date('Ym')]);
                }
                $data[$k]['month_sale'] = (int)$v['month_sale'];
                $data[$k]['min_express_charge'] = (int)$v['min_express_charge'];
                $data[$k]['new_user_reduction'] = (string)$v['new_user_reduction'];
                $data[$k]['distance'] = (string)($this->getDistance($lat, $lng, $v['lat'], $v['lng']) / 1000);  //距离
				$data[$k]['express_time'] = '40分钟';  //配送时间
                $data[$k]['introduce'] = (string)$v['introduce'];
                $data[$k]['phone'] = (string)$v['phone'];
                $data[$k]['address'] = (string)$v['address'];

                $express_config = Db::table('express_config')->where('aid',$v['aid'])->value('charge');
                $express_charge = $data[$k]['distance']*$express_config;
                if($express_charge < $express_config){
                    $express_charge = $express_config;
                }
                $data[$k]['express_charge'] = (string)$express_charge;

                unset($data[$k]['lng']);
                unset($data[$k]['lat']);
                $money_off = Db::table('money_off')->where('seller_id', $v['seller_id'])->select();
                $string = '';
                foreach ($money_off as $k2 => $v2) {
                    $string .= '满' . $v2['full'] . '减' . $v2['minus'] . ';';
                }
                if (!empty($v['new_user_reduction'])) {
                    $data[$k]['new_user_reduction'] = '新用户下单立减' . $v['new_user_reduction'];
                } else {
                    $data[$k]['new_user_reduction'] = '';
                }
                $data[$k]['money_off'] = (string)$string;
                $data[$k]['isFavourite'] = (int)Db::table('favourite')->where(['uid' => $uid, 'seller_id' => $v['seller_id']])->count();
            }
            $msg = '获取成功';
        }
        return json(['state' => $state, 'data' => $data, 'msg' => $msg]);
    }

    //根据销量排序商家列表
    public function high_sales()
    {
        $uid = token_decode(req('token'));
        $where = get_pca();
        $where['status'] = 1;
        $lng = req('lng');
        $lat = req('lat');
        $cate_id = req('cate_id');
        if ($cate_id) {
            $where['cate_id'] = $cate_id;
        }

        $whereIn = '';
        $whereIn_field = null;
        $active = req('active');
        $consume = req('consume');
        $attr = req('attr');
        if (!empty($active)) {
            if ($active == 'money_off') {
                $whereIn_field = 'id';
                $money_off_seller = Db::table('money_off')->group('seller_id')->select();
                foreach ($money_off_seller as $index => $item) {
                    $whereIn .= $item['seller_id'] . ',';
                }
                $whereIn = substr($whereIn, 0, strlen($whereIn) - 1);
            } elseif ($active == 'present') {
                $whereIn_field = 'id';
                $money_off_seller = Db::table('service')->where('present!=""')->group('seller_id')->select();
                foreach ($money_off_seller as $index => $item) {
                    $whereIn .= $item['seller_id'] . ',';
                }
                $whereIn = substr($whereIn, 0, strlen($whereIn) - 1);
            } elseif ($active == 'new_user_reduction') {
                $where[$active] = ['<>', ''];
            }
        }
        if (!empty($consume)) {
            $where['consume'] = explode('&', $consume);
        }
        if (!empty($attr)) {
            if ($attr == 'brand') {
                $where['label'] = '品牌';
            } elseif ($attr == 'advance') {
                $where['advance'] = 1;
            }
        }

        $fields = 'id as seller_id,aid,volume_date,headimg,name,min_express_charge,lng,lat,new_user_reduction,volume as month_sale,label,business,introduce,phone,address,advance';
        $page = req('page') ? req('page') : 1;
        $page_size = req('page_size') ? req('page_size') : 10;
        $data = Db::table('seller')->field($fields)->whereIn($whereIn_field, $whereIn)->where($where)->order('volume desc')->page($page, $page_size)->select();
        foreach ($data as $k => $v) {
			$data[$k]['grade'] = Db::table('evaluate')->where('seller_id', $v['seller_id'])->where('time>complete_time or complete_time<=' . time() - 30000)->avg('star');
			$data[$k]['grade'] = $data[$k]['grade']?(string)(round($data[$k]['grade'], 1)):'5';
            if($v['volume_date'] != date('Ym')){
                Db::table('seller')->where('id',$v['seller_id'])->update(['volume'=>0,'volume_date'=>date('Ym')]);
            }
            $data[$k]['month_sale'] = (int)$v['month_sale'];
            $data[$k]['min_express_charge'] = (int)$v['min_express_charge'];
            $data[$k]['new_user_reduction'] = (string)$v['new_user_reduction'];
            $data[$k]['distance'] = (string)($this->getDistance($lat, $lng, $v['lat'], $v['lng']) / 1000);  //距离
			$data[$k]['express_time'] = '40分钟';  //配送时间
            $data[$k]['introduce'] = (string)$v['introduce'];
            $data[$k]['phone'] = (string)$v['phone'];
            $data[$k]['address'] = (string)$v['address'];

            $express_config = Db::table('express_config')->where('aid',$v['aid'])->value('charge');
            //计算配送费
			$pi = $data[$k]['distance'];
			$express_charge = $express_config['charge'];
			if($pi > $express_config['scope']){
				$express_charge += $express_config['add_charge']*($pi-$express_config['scope']);
			}
			if ($express_charge < $express_config['charge']) {
				$express_charge = $express_config['charge'];
			}
			$data[$k]['express_charge'] = (string)round($express_charge);

            unset($data[$k]['lng']);
            unset($data[$k]['lat']);
            $money_off = Db::table('money_off')->where('seller_id', $v['seller_id'])->select();
            $string = '';
            foreach ($money_off as $k2 => $v2) {
                $string .= '满' . $v2['full'] . '减' . $v2['minus'] . ';';
            }
            if (!empty($v['new_user_reduction'])) {
                $data[$k]['new_user_reduction'] = '新用户下单立减' . $v['new_user_reduction'];
            }
            $data[$k]['money_off'] = (string)$string;
            $data[$k]['isFavourite'] = (int)Db::table('favourite')->where(['uid' => $uid, 'seller_id' => $v['seller_id']])->count();
        }
        return json(['state' => 1, 'data' => $data, 'msg' => '获取成功']);
    }

    //收藏和取消收藏商家
    public function edit_favourite()
    {
        $uid = token_decode(req('token'));
        $seller_id = req('seller_id');
        $status = (int)req('status');
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid) || empty($seller_id)) {
            $msg['msg'] = '参数错误';
        } else {
            if ($status == 1) {
                $count = Db::table('favourite')->where(['uid' => $uid, 'seller_id' => $seller_id])->count();
                if ($count == 0) {
                    Db::table('favourite')->insert(['uid' => $uid, 'seller_id' => $seller_id, 'time' => time()]);
                }
            } else {
                Db::table('favourite')->where(['uid' => $uid, 'seller_id' => $seller_id])->delete();
            }
            $msg['state'] = 1;
            $msg['msg'] = '操作完成';
        }
        return json($msg);
    }

    //获取收藏商家列表
    public function get_favourite()
    {
        $uid = token_decode(req('token'));
        $lng = req('lng');
        $lat = req('lat');
        $msg['state'] = 0;
        $msg['data'] = [];
        if (empty($uid)) {
            $msg['msg'] = '请先登录';
        } else {
            $msg['state'] = 1;
            $fields = 'id as seller_id,aid,volume_date,headimg,name,min_express_charge,lng,lat,new_user_reduction,volume as month_sale,label,business,introduce,phone,address,advance';
            $page = req('page') ? req('page') : 1;
            $page_size = req('page_size') ? req('page_size') : 10;
            $data = Db::table('favourite f,seller s')->field($fields)->where('f.uid', $uid)->where('s.status', 1)->where('f.seller_id = s.id')->page($page, $page_size)->order('time desc')->select();
            foreach ($data as $k => $v) {
                $data[$k]['grade'] = Db::table('evaluate')->where('seller_id', $v['seller_id'])->where('time>complete_time or complete_time<=' . time() - 30000)->avg('star');
				$data[$k]['grade'] = $data[$k]['grade']?(string)(round($data[$k]['grade'], 1)):'5';
                if($v['volume_date'] != date('Ym')){
                    Db::table('seller')->where('id',$v['seller_id'])->update(['volume'=>0,'volume_date'=>date('Ym')]);
                }
                $data[$k]['month_sale'] = (int)$v['month_sale'];
                $data[$k]['min_express_charge'] = (int)$v['min_express_charge'];
                $data[$k]['new_user_reduction'] = (string)$v['new_user_reduction'];
                $data[$k]['distance'] = (string)($this->getDistance($lat, $lng, $v['lat'], $v['lng']) / 1000);  //距离
				$data[$k]['express_time'] = '40分钟';  //配送时间
                $data[$k]['introduce'] = (string)$v['introduce'];
                $data[$k]['phone'] = (string)$v['phone'];
                $data[$k]['address'] = (string)$v['address'];

                $express_config = Db::table('express_config')->where('aid',$v['aid'])->value('charge');
                //计算配送费
				$pi = $data[$k]['distance'];
                $express_charge = $express_config['charge'];
				if($pi > $express_config['scope']){
					$express_charge += $express_config['add_charge']*($pi-$express_config['scope']);
				}
				if ($express_charge < $express_config['charge']) {
					$express_charge = $express_config['charge'];
				}
				$data[$k]['express_charge'] = (string)round($express_charge);

                unset($data[$k]['lng']);
                unset($data[$k]['lat']);
                $money_off = Db::table('money_off')->where('seller_id', $v['seller_id'])->select();
                $string = '';
                foreach ($money_off as $k2 => $v2) {
                    $string .= '满' . $v2['full'] . '减' . $v2['minus'] . ';';
                }
                if (!empty($v['new_user_reduction'])) {
                    $data[$k]['new_user_reduction'] = '新用户下单立减' . $v['new_user_reduction'];
                }
                $data[$k]['money_off'] = (string)$string;
                $data[$k]['isFavourite'] = (int)Db::table('favourite')->where(['uid' => $uid, 'seller_id' => $v['seller_id']])->count();
            }
            $msg['msg'] = '获取成功';
            $msg['data'] = $data;
        }
        return json($msg);
    }

    //获取商家评价列表
    public function get_evaluate_list()
    {
        $seller_id = req('seller_id');
        $page = req('page') ? req('page') : 1;
        $page_size = req('page_size') ? req('page_size') : 10;
        $data = Db::table('evaluate')->field('uid,star,text,image1,image2,time')->where(['seller_id'=> $seller_id,'type'=>'seller'])->where('time>complete_time or complete_time<=' . time() - 30000)->page($page, $page_size)->select();
        foreach ($data as $index => $datum) {
            unset($data[$index]['uid']);
            $user = Db::table('user')->field('headimg,name')->where('id', $datum['uid'])->find();
            $data[$index] = array_merge($data[$index], $user);
        }
        return json(['state' => 1, 'data' => $data, 'msg' => '获取成功']);
    }

    //分享商家h5页面
    public function share()
    {
        $seller_id = req('seller_id');
        $data = Db::table('seller')->where('id', $seller_id)->find();
        $money_off = Db::table('money_off')->where('seller_id', $seller_id)->select();
        $goods = Db::table('service')->where('uid', $seller_id)->order('volume desc')->select();
        $string = '';
        foreach ($money_off as $k2 => $v2) {
            $string .= '满' . $v2['full'] . '减' . $v2['minus'] . ';';
        }
        $data['goods'] = $goods;
        $data['money_off'] = $string;
        return view('', ['data' => $data]);
    }

    /**************************商家**************************/

    //商家修改信息
    public function set_info()
    {
        $uid = token_decode(req('token'));
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid)) {
            $msg['msg'] = '参数错误';
        } else {
            $business = req('business');
            if ($business == 1 || $business == 0) {
                Db::table('seller')->where('id', $uid)->update(['business'=>$business]);
            }
            if ($business != 0) {
                $advance_receive_count = Db::table('order')->where(['seller_id' => $uid, 'status'=>2,'advance_receive' => 0])->count();
                if($advance_receive_count > 0){
                    push(md5($uid), json_encode(['type' => 4, 'message' => '您有新的订单可以接单啦~', 'parameters' => '']), 'seller');
                    insert_message('seller', $uid, '订单提醒', '您有新的订单可以接单啦~', '4', '');
                }
            }
            $msg['data'] = Db::table('seller')->field('headimg,name,business,introduce,address')->where('id', $uid)->find();
            $msg['state'] = 1;
            $msg['msg'] = '操作成功';
        }
        return json($msg);
    }

    //商家获取、编辑配置信息
    public function settings()
    {
        $uid = token_decode(req('token'));
        $data['introduce'] = req('introduce');
//        $data['min_express_charge'] = req('min_express_charge');
        $data['new_user_reduction'] = req('new_user_reduction');
        $data['consume'] = req('consume');
        /*if ($data['new_user_reduction'] == 0) {
            $data['new_user_reduction'] = '';
        }*/
        $handle = req('handle');
        $msg['state'] = 0;
        $msg['data'] = (object)[];
        if (empty($uid)) {
            $msg['msg'] = '请先登录';
        } else {
            $msg['state'] = 1;
            $msg['msg'] = '操作完成';
            if ($handle == 'edit') {
                Db::table('seller')->where('id', $uid)->update($data);
            }
            $msg['data'] = Db::table('seller')->field('headimg,name,address,introduce,min_express_charge,new_user_reduction')->where('id', $uid)->find();
        }
        return json($msg);
    }

    //获取编辑商品分类
    public function category()
    {
        $uid = token_decode(req('token'));
        $cate_id = req('cate_id');
        $title = req('title');
        $handle = req('handle');
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid)) {
            $msg['msg'] = '请先登录';
        } else {
            $msg['state'] = 1;
            $info = Db::table('seller')->field('pid,cid,aid,id as uid')->where('id', $uid)->find();
            if ($handle == 'add' && $title) {
                $info['title'] = $title;
                Db::table('service_category')->insert($info);
            } elseif ($handle == 'edit' && $cate_id && $title) {
                Db::table('service_category')->where(['uid' => $uid, 'id' => $cate_id])->update(['title' => $title]);
            } elseif ($handle == 'del' && $cate_id) {
                Db::table('service_category')->where(['uid' => $uid, 'id' => $cate_id])->delete();
                Db::table('service')->where(['uid' => $uid, 'cate_id' => $cate_id])->delete();
            }
            $msg['data'] = Db::table('service_category')->field('id as cate_id,title')->where('uid', $uid)->order('id asc')->select();
            $msg['msg'] = '操作成功';
        }
        return json($msg);
    }

    //获取商品列表
    public function goods_list()
    {
        $uid = token_decode(req('token'));
        $msg['state'] = 0;
        $msg['data'] = [];
        if (empty($uid)) {
            $msg['msg'] = '请先登录';
        } else {
            $msg['state'] = 1;
            $data = Db::table('service')->field('id as goods_id,cate_id,title,image1,price,box_charge,present,service,status,volume as month_sale,discount')->where('uid', $uid)->select();
            foreach ($data as $index => $value) {
                $data[$index]['cate_name'] = Db::table('service_category')->where('id', $value['cate_id'])->value('title');
            }
            $msg['data'] = $data;
            $msg['msg'] = '获取成功';
        }
        return json($msg);
    }

    //添加编辑商品
    public function edit_goods(){
        $uid = token_decode(req('token'));
        $goods_id = req('goods_id');
        $data = Db::table('seller')->field('pid,cid,aid')->where('id',$uid)->find();
        $data['cate_id'] = req('cate_id');
        $data['title'] = req('title');
        $data['price'] = req('price');
        $data['status'] = req('status');
        $data['required'] = req('required');
        $spec = json_decode(req('spec'),true);
        $handle = req('handle');
        $msg['state'] = 0;
        $msg['data'] = '';
        if (!$uid) {
            $msg['msg'] = '请先登录';
        } else {
            $data['uid'] = $uid;
            if ($handle == 'add') {
                $bool = true;
                foreach ($data as $k => $v) {
                    if ($v == '') {
                        $msg['msg'] = '请填写完整信息';
                        $bool = false;
                        break;
                    }
                }
                $data['box_charge'] = req('box_charge');
                $data['present'] = req('present');
                $data['service'] = req('service');
                $data['discount'] = req('discount');
                if ($bool) {
                    if ($_FILES) {
                        $files = upload();
                        $data['image1'] = $files['image1']['url'];
                        $msg['state'] = 1;
                        $id = Db::table('service')->insertGetId($data);
						if(!empty($spec)){
							$ids = array();
							$speclist = Db::table("service_spec")->field('id')->where(['service_id' => $id])->select();
							foreach ($speclist as $v) $ids[] = $v['id'];
							foreach ($spec as $sp) {
								if (empty($sp['title'])) continue;
								$f_data = array(
									'title' => $sp['title'],
									'service_id' => $id,
									'price' => $sp['price']
								);
								if ($sp['id'] && in_array($sp['id'], $ids)) {
									Db::table("service_spec")->where(['id' => $sp['id']])->update($f_data);
									$ids = array_diff($ids, array($sp['id']));
								} else {
									Db::table("service_spec")->insert($f_data);
								}
							}
							if ($ids) {
								Db::table("service_spec")->where(['service_id' => $id])->delete($ids);
							}
						}
                        $msg['msg'] = '操作完成';
                    } else {
                        $msg['msg'] = '请上传商品图片';
                    }
                }
            } elseif ($handle == 'edit') {
                if (!$goods_id) {
                    $msg['msg'] = '请选择商品';
                } else {
                    if ($_FILES) {
                        $files = upload();
                        $data['image1'] = $files['image1']['url'];
                    }
                    unset($data['price']);
                    unset($data['discount']);
                    unset($data['required']);
                    $msg['state'] = 1;
                    Db::table('service')->where(['uid' => $uid, 'id' => $goods_id])->update($data);
                    $msg['msg'] = '操作完成';
                }
            } else {
                $msg['msg'] = 'what do you want to do?';
            }
        }
        return json($msg);
    }

    //删除商品
    public function del_goods()
    {
        $uid = token_decode(req('token'));
        $goods_id = req('gid');
        $msg['state'] = 0;
        $msg['data'] = '';
        if (!$uid || !$goods_id) {
            $msg['msg'] = '请先登录';
        } else {
            $msg['state'] = 1;
            Db::table('service')->where(['uid' => $uid, 'id' => $goods_id])->delete();
            $msg['msg'] = '操作完成';
        }
        return json($msg);
    }

    //商品上下架
    public function set_goods_status()
    {
        $uid = token_decode(req('token'));
        $goods_id = req('goods_id');
        $status = req('status');
        $msg['state'] = 0;
        if (empty($uid) || empty($goods_id) || ($status != 0 && $status != 1)) {
            $msg['msg'] = '参数错误';
        } else {
            $msg['state'] = 1;
            Db::table('service')->where(['id' => $goods_id, 'uid' => $uid])->update(['status' => $status]);
            $msg['data'] = $status;
            $msg['msg'] = '操作完成';
        }
        return json($msg);
    }

    //获取满减优惠列表
    public function get_money_off()
    {
        $uid = token_decode(req('token'));
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid)) {
            $msg['msg'] = '请先登录';
        } else {
            $msg['state'] = 1;
            $msg['data'] = Db::table('money_off')->field('id,full,minus')->where('seller_id', $uid)->select();
            $msg['msg'] = '操作成功';
        }
        return json($msg);
    }

    //编辑满减优惠
    public function edit_money_off()
    {
        $uid = token_decode(req('token'));
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid)) {
            $msg['msg'] = '请先登录';
        } else {
            $msg['state'] = 1;
            $data['seller_id'] = $uid;
            $data['full'] = req('full');
            $data['minus'] = req('minus');
            $data['system_minus'] = 0;
            $data['seller_minus'] = req('minus');
            Db::table('money_off')->insert($data);
            $msg['msg'] = '操作成功';
        }
        return json($msg);
    }

    //删除满减优惠
    public function del_money_off()
    {
        $uid = token_decode(req('token'));
        $off_id = req('off_id');
        $msg['state'] = 0;
        $msg['data'] = '';
        if ($off_id == '') {
            $msg['msg'] = '参数错误';
        } else {
            $msg['state'] = 1;
            Db::table('money_off')->where(['off_id' => $off_id, 'seller_id' => $uid])->delete();
            $msg['msg'] = '删除成功';
        }
        return json($msg);
    }

    //商家获取订单
    public function order_list()
    {
        $uid = token_decode(req('token'));
        $status = req('status');
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid)) {
            $msg['msg'] = '请先登录';
        } else {
            $msg['state'] = 1;
            $page = !empty(req('page')) ? req('page') : 1;
            $page_size = !empty(req('page_size')) ? req('page_size') : 10;
            $fields = 'order_id,seller_order_id,title,lng,lat,address,name,phone,remark,last_price,express_charge,sum_price,new_user_reduction,money_off_text,money_off,red_packet,status,advance,advance_receive,payment,time,pay_time,express_receive_time,seller_receive_time,express_time,complete_time,back_time,cancel_time,express_id';
            $data = [];
            if ($status == 1) {
                $msg['count'] = Db::table('order')->field($fields)->where(['seller_id' => $uid, 'advance_receive' => 0,'status'=>2])->count();
                $data = Db::table('order')->field($fields)->where(['seller_id' => $uid, 'advance_receive' => 0,'status'=>2])->page($page, $page_size)->order('time asc')->select();
            } elseif ($status == 2) {
                $msg['count'] = Db::table('order')->field($fields)->where(['seller_id' => $uid, 'advance_receive' => 1, 'status' => ['between', '2,4']])->count();
                $data = Db::table('order')->field($fields)->where(['seller_id' => $uid, 'advance_receive' => 1, 'status' => ['between', '2,4']])->page($page, $page_size)->order('time asc')->select();
            } elseif ($status == 3) {
                $msg['count'] = Db::table('order')->field($fields)->where(['seller_id' => $uid, 'advance_receive' => 1, 'status' => ['between', '5,7']])->count();
                $data = Db::table('order')->field($fields)->where(['seller_id' => $uid, 'advance_receive' => 1, 'status' => ['between', '5,7']])->page($page, $page_size)->order('time asc')->select();
            }
            foreach ($data as $k => $v) {
                $goods = Db::table('order_detail')->field('title,image,price,service,number,price,box_charge')->where('order_id', $v['order_id'])->select();
                $goods_total = 0;
                $goods_price = 0;
                $box_charge = 0;
                foreach ($goods as $gk => $gv) {
                    // $service = Db::table('service')->where('id', $gv['goods_id'])->find();
                    // $goods[$gk]['title'] = $service['title'];
                    // $goods[$gk]['image'] = $service['image1'];
                    // $goods[$gk]['service'] = $service['service'];
                    // $goods[$gk]['price'] = $service['price'];
                    $goods_total += $gv['number'];
                    $goods_price += $gv['number'] * $gv['price'];
                    $box_charge += $gv['number'] * $gv['box_charge'];
                    unset($goods[$gk]['box_charge']);
                }
                $data[$k]['goods_total'] = (string)$goods_total;
                $data[$k]['goods_price'] = (string)$goods_price;
                $data[$k]['box_charge'] = (string)$box_charge;

                if (!empty($v['express_id'])) {
                    $express = Db::table('express')->where('id', $v['express_id'])->find();
                    $data[$k]['express_headimg'] = $express['headimg'];
                    $data[$k]['express_phone'] = $express['phone'];
                    $data[$k]['express_name'] = $express['name'];
                    $data[$k]['express_lng'] = $express['lng'];
                    $data[$k]['express_lat'] = $express['lat'];
                } else {
                    $data[$k]['express_headimg'] = '';
                    $data[$k]['express_phone'] = '';
                    $data[$k]['express_name'] = '';
                    $data[$k]['express_lng'] = '';
                    $data[$k]['express_lat'] = '';
                }
                $data[$k]['goods'] = $goods;
            }
            $msg['data'] = $data;
            $msg['msg'] = '获取成功';
        }
        return json($msg);
    }

    //商家接受或取消预约订单
    public function receive_advance_order()
    {
        $uid = token_decode(req('token'));
        $order_id = req('order_id');
        $status = req('status');
        $msg['state'] = 0;
        $msg['data'] = '';
        if (!$uid || !$order_id || $status == '') {
            $msg['msg'] = '参数错误';
        } else {
            $order = Db::table('order')->where('order_id', $order_id)->find();
            if ($status == 1) {
							if($order['instore'] == 1){
                if (Db::table('order')->where(['seller_id' => $uid, 'order_id' => $order_id, 'advance_receive' => 0])->update(['advance_receive' => 1, 'seller_receive_time' => time()])) {
									$msg['state'] = 1;
									//获取配送方式
									$express_type = Db::table("setting")->where(['id'=>1])->value('express_type');
									if($express_type == 1){
										//排除有单人员
										$express_not_array = Db::table('order')->where(['status'=>['between','3,4']])->select();
										$express_not_string = '';
										foreach ($express_not_array as $item) {
												$express_not_string .= $item['express_id'].',';
										}
										$express_not_string = substr($express_not_string,0,strlen($express_not_string)-1);

										//推给本城市配送员,排除有单人员
										$express_array = Db::table('express')->where(['aid'=>$order['aid'],'business'=>1])->whereNotIn('id',$express_not_string)->select();
										$express_push = [];
										foreach ($express_array as $v){
												$express_push[] = md5($v['id']);
										}
										push(md5($order['uid']), json_encode(['type' => 2, 'message' => '商家已接单，等待骑手接单~', 'parameters' => $order_id]), 'user');
										push($express_push, json_encode(['type' => 5, 'message' => '有新的订单可以抢单哦~', 'parameters' => $order_id]), 'express');
									}else{
										//推送达达配送骑士
										vendor('dadaps.Dadaps#Api');
										$dadaApi = new \DadapsApi;
										$params = $order;
										$params['shop_no'] = Db::table("seller")->where(['id'=>$uid])->value('dada_shop_id');
										$result = $dadaApi->addOrder($params);
										if($result['status'] == 1){
											push(md5($order['uid']), json_encode(['type' => 2, 'message' => '商家已接单，等待骑手接单~', 'parameters' => $order_id]), 'user');
										}else{
											Db::table('order')->where(['seller_id' => $uid, 'order_id' => $order_id])->update(['advance_receive' => 0, 'seller_receive_time' => '']);
											$msg['state'] = 0;
											$msg['msg'] = '系统繁忙，请稍后再试';
										}
									}
                }
							}else{
								
								//完成订单
								$update['complete_time'] = time();

								//给商家和配送员加钱
								$seller = Db::table('seller')->field('cut,aid,volume,comode')->where('id', $order['seller_id'])->find();
								if($seller['comode'] == 0){
									if(!empty($order['money_off'])){
										$order['goods_price'] = $order['goods_price'] - $order['seller_money_off'];
									}
									$order['goods_price'] = $order['goods_price'] + $order['box_charge'];
									$seller_money = $order['goods_price'] - ($order['goods_price'] * $seller['cut'] / 100);
														}else{
									$order['goods_price'] = $order['goods_price'] + $order['box_charge'];
									$seller_money = $order['goods_price'] - ($order['goods_price'] * $seller['cut'] / 100);
									if(!empty($order['money_off'])){
										$order['goods_price'] = $order['goods_price'] - $order['seller_money_off'];
									}
								}
								$seller_money = round($seller_money,2);
								Db::table('seller')->where('id', $order['seller_id'])->setInc('money', $seller_money);
								Db::table('earning')->insert(['order_id' => $order_id, 'uid' => $order['seller_id'], 'money' => $seller_money, 'time' => time(), 'type' => 'seller']);
								//push用户
								push(md5($order['uid']), json_encode(['type' => 2, 'message' => '您的订单已完成', 'parameters' => $order_id]), 'user');

								//自动评价预启动
								Db::table('evaluate')->insert(['order_id'=>$order_id,'seller_id'=>$order['seller_id'],'uid'=>$order['uid'],'star'=>'5.0','text'=>'系统默认好评','time'=>time(),'complete_time'=>time()]);
								Db::table('evaluate')->insert(['order_id'=>$order_id,'express_id'=>$uid,'uid'=>$order['uid'],'star'=>'5.0','text'=>'系统默认好评','time'=>time(),'complete_time'=>time()]);

								//push商家
								push(md5($order['seller_id']), json_encode(['type' => 2, 'message' => '订单已完成，本次收益'.$seller_money.'元', 'parameters' => $order_id]), 'seller');

								//销量
								$volume = $seller['volume'];
								$goods = Db::table('order_detail')->where('order_id', $order['order_id'])->select();
								foreach ($goods as $k => $v) {
										$volume += $v['number'];
										$goods_volume = (int)Db::table('service')->where('id',$v['goods_id'])->value('volume');
										Db::table('service')->where(['id' => $v['goods_id'], 'uid' => $v['seller_id']])->update(['volume'=> $goods_volume+$v['number'],'volume_date'=>date('Ym')]);
								}
								Db::table('seller')->where('id',$order['seller_id'])->update(['volume'=>$volume,'volume_date'=>date('Ym')]);

								$user_order_count = Db::table('order')->where(['order_id'=>['<>',$order_id],'uid'=>$order['uid'],'status'=>5])->count();
								$invitation = Db::table('user')->where(['id'=>$order['uid'],'status'=>1])->value('invite');
								//邀请送好礼
								if (!empty($invitation) && $user_order_count == 0) {
									$where['aid'] = $order['aid'];
									$where['status'] = 1;
									$where['title'] = '邀请送好礼';
									$active = Db::table('active')->where($where)->find();
									if (!empty($active['redpacket'])) {
										$this->grant_redpacket($invitation,$active['redpacket'],'邀请红包',0);
									}
								}
								//三级分销
								$setting = Db::table("setting")->where(['id'=>1])->find();
								//一级
								if(!empty($invitation) && $setting['level_1']>0){
									$commision_money = round($order['last_price']/100*$setting['level_1'],2);
									if(Db::table("user")->where(['id'=>$invitation,'status'=>1])->setInc('money', $commision_money)){
										Db::table('earning')->insert(['order_id' => $order_id, 'uid' => $invitation, 'money' => $commision_money, 'time' => time(), 'type' => 'user']);
										$commision = $commision + $commision_money;
									}
									//二级
									if(!empty($invitation) && $setting['level_2']>0){
										$invitation = Db::table('user')->where(['id'=>$invitation,'status'=>1])->value('invite');
										$commision_money = round($order['last_price']/100*$setting['level_2'],2);
										if(Db::table("user")->where(['id'=>$invitation,'status'=>1])->setInc('money', $commision_money)){
											Db::table('earning')->insert(['order_id' => $order_id, 'uid' => $invitation, 'money' => $commision_money, 'time' => time(), 'type' => 'user']);
											$commision = $commision + $commision_money;
											//三级
											if(!empty($invitation) && $setting['level_3']>0){
												$invitation = Db::table('user')->where(['id'=>$invitation,'status'=>1])->value('invite');
												$commision_money = round($order['last_price']/100*$setting['level_3'],2);
												if(Db::table("user")->where(['id'=>$invitation,'status'=>1])->setInc('money', $commision_money)){
										                       Db::table('earning')->insert(['order_id' => $order_id, 'uid' => $invitation, 'money' => $commision_money, 'time' => time(), 'type' => 'user']);
												      $commision = $commision + $commision_money;
											       }
											}
										}
									}
								}
								//算平台钱+不算配送费
								//$update['system_money'] = $order['last_price']-$seller_money-$express_money;
								$update['system_money'] = $order['last_price']-$seller_money-$commision;
								$update['status'] = 4;
                                                                Db::table('order')->where('order_id', $order_id)->update($update);
								$msg['state'] = 1;
								$msg['msg'] = '操作完成';
							}
               }elseif ($status == 0) {
                //取消订单，推送用户
                  if (Db::table('order')->where(['seller_id' => $uid, 'order_id' => $order_id])->update(['advance_receive' => 0, 'status' => 7])) {
                     $msg['state'] = 1;
                     push(md5($order['uid']), json_encode(['type' => 2, 'message' => '很抱歉，商家取消了您的订单', 'parameters' => $order_id]), 'user');
                     if (!empty($order['express_id'])) {
                         push(md5($order['express_id']), json_encode(['type' => 2, 'message' => '很抱歉，商家取消了订单', 'parameters' => $order_id]), 'express');
                     }
                     //取消订单，退还优惠券
                     Db::table('order')->where(['uid' => $uid, 'order_id' => $order_id])->update(['status' => 7, 'cancel_time' => time()]);
                     if (!empty($order['red_id'])) {
                         Db::table('red_packet')->where(['uid' => $uid, 'id' => $order['red_id']])->setDec('flag');
                     }
                    //退款功能
                    if ($order['payment'] == 'wxpay'){
                        //微信
                        vendor('wxpayv_app.WxPay#Api');
                        $input = new \WxPayRefund();
                        $mchid = \WxPayConfig::$mchid;
                        $input->SetOut_trade_no($order['order_id']);         //自己的订单号
                        $input->SetTransaction_id(null);     //微信官方生成的订单流水号，在支付成功中有返回
                        $input->SetOut_refund_no($order['order_id'] . '_exit');         //退款单号
                        $input->SetTotal_fee($order['last_price'] * 100);         //订单标价金额，单位为分
                        $input->SetRefund_fee($order['last_price'] * 100);            //退款总金额，订单总金额，单位为分，只能为整数
                        $input->SetOp_user_id($mchid);
                        $info = \WxPayApi::refund($input);
                        if ($info['return_code'] == 'SUCCESS') {
                            //退款申请成功
                            $pay_bool = true;
                            $msg['state'] = 1;
                            $msg['msg'] = '操作成功';
                        } else {
                            //失败
                            $msg['msg'] = '系统繁忙';
                        }

                    }
                    $msg['msg'] = '操作完成';
                } else {
                    $msg['msg'] = '系统繁忙';
                }
            }
        }
        return json($msg);
    }

    //收益明细
    public function earning_detail()
    {
        $token = req('token');
        $uid = token_decode($token);
        $msg['state'] = 0;
        $msg['data'] = [];
        if (empty($uid)) {
            $msg['msg'] = '参数错误';
        } else {
            $msg['state'] = 1;
            $msg['count'] = Db::table('earning')->where('uid', $uid)->count();
            $page = !empty(req('page')) ? req('page') : 1;
            $pagesize = !empty(req('pagesize')) ? req('pagesize') : 10;
            $msg['data'] = Db::table('earning')->field('order_id,money,time')->where(['uid' => $uid, 'type' => 'seller'])->page($page, $pagesize)->order('time desc')->select();
            $msg['msg'] = '获取成功';
        }
        return json($msg);
    }

    //申请提现
    public function losemoney()
    {
        $uid = token_decode(req('token'));
        $seller = Db::table('seller')->where('id', $uid)->find();
        $money = $seller['money'];
        $msg['state'] = 0;
        $msg['data'] = '';
        if ($seller['money'] < $money || $money < 100) {
            $msg['msg'] = '很抱歉，您的余额不满足提现条件';
        } else {
            if ($seller['losemoney'] != 0) {
                $msg['msg'] = '您上一个提现处理还未完成，无法再进行提现';
            } else {
                $post['uid'] = $uid;
                $post['pid'] = $seller['pid'];
                $post['cid'] = $seller['cid'];
                $post['aid'] = $seller['aid'];
                $post['status'] = 2;
                $post['type'] = 'seller';
                $post['truename'] = $seller['truename'];
                $post['account'] = $seller['bank_card'];
                $post['money'] = $seller['money'];
                $post['lose_time'] = time();
                Db::table('losemoney')->insert($post);
                Db::table('seller')->where('id', $uid)->update(['money' => $seller['money'] - $money, 'losemoney' => $money]);
                $msg['state'] = 1;
                $msg['msg'] = '提现申请成功';
            }
        }
        return json($msg);
    }

    //获取提现记录
    public function lose_info()
    {
        $uid = token_decode(req('token'));
        $msg['state'] = 0;
        $msg['data'] = [];
        if (empty($uid)) {
            $msg['msg'] = '参数错误';
        } else {
            $msg['state'] = 1;
            $msg['msg'] = '获取成功';
            $msg['count'] = Db::table('losemoney')->where(['uid' => $uid, 'type' => 'seller'])->count();
            $fields = 'money,status,lose_time as losttime';
            $page = !empty(req('page')) ? req('page') : 1;
            $pagesize = !empty(req('pagesize')) ? req('pagesize') : 10;
            $msg['data'] = Db::table('losemoney')->field($fields)->where(['uid' => $uid, 'type' => 'seller'])->page($page, $pagesize)->order('lose_time desc')->select();
        }
        return json($msg);
    }

    //获取账户余额
    public function get_balance()
    {
        $uid = token_decode(req('token'));
        $msg['state'] = 0;
        $msg['data'] = (object)[];
        if (empty($uid)) {
            $msg['msg'] = '参数错误';
        } else {
            $msg['state'] = 1;
            $msg['data'] = [];
            $msg['data']['money'] = Db::table('seller')->where('id', $uid)->value('money');
            $msg['msg'] = '获取成功';
        }
        return json($msg);
    }

    //获取商家详情
    public function get_detail()
    {
        $uid = token_decode(req('token'));
        $seller_id = req('seller_id');
        $lng = req('lng');
        $lat = req('lat');
        $msg['state'] = 0;
        $msg['data'] = (object)[];
        if (empty($seller_id)) {
            $msg['msg'] = '参数错误';
        } else {
            $msg['state'] = 1;
            $fields = 'id as seller_id,aid,headimg,range,detail_image1,detail_image2,detail_image3,name,min_express_charge,lng,lat,new_user_reduction,volume as month_sale,label,business,introduce,phone,address,advance';
            $data = Db::table('seller')->field($fields)->where('id', $seller_id)->find();
			$ranges = json_decode($data['range']);
			$newranges = array();
			foreach($ranges as $range){
				$range_n = array(); 
				$range_n['lng'] = $range[0];
				$range_n['lat'] = $range[1];
				$newranges[] = (object)$range_n;
			}
			$data['range'] = $newranges;
			//var_dump($newranges);exit;
			$data['grade'] = Db::table('evaluate')->where('seller_id', $data['seller_id'])->where('time>complete_time or complete_time<=' . time() - 30000)->avg('star');
			$data['grade'] = (string)(round($data['grade'], 1));
            $data['month_sale'] = (int)$data['month_sale'];
            $data['min_express_charge'] = (int)$data['min_express_charge'];
            $data['new_user_reduction'] = (string)$data['new_user_reduction'];
            $data['distance'] = (string)($this->getDistance($lat, $lng, $data['lat'], $data['lng']) / 1000);  //距离
			$data['express_time'] = '40分钟';  //配送时间
            $data['introduce'] = (string)$data['introduce'];
            $data['phone'] = (string)$data['phone'];
            $data['address'] = (string)$data['address'];

            $express_config = Db::table('express_config')->where('aid',$data['aid'])->value('charge');
            //计算配送费
			$pi = $data['distance'];
			$express_charge = $express_config['charge'];
			if($pi > $express_config['scope']){
				$express_charge += $express_config['add_charge']*($pi-$express_config['scope']);
			}
			if ($express_charge < $express_config['charge']) {
				$express_charge = $express_config['charge'];
			}
			$data['express_charge'] = (string)round($express_charge);

            $money_off = Db::table('money_off')->where('seller_id', $data['seller_id'])->select();
            $string = '';
            foreach ($money_off as $k2 => $v2) {
                $string .= '满' . $v2['full'] . '减' . $v2['minus'] . ';';
            }
            if (!empty($data['new_user_reduction'])) {
                $data['new_user_reduction'] = '新用户下单立减' . $data['new_user_reduction'];
            }
            $data['money_off'] = (string)$string;
            $data['isFavourite'] = (int)Db::table('favourite')->where(['uid' => $uid, 'seller_id' => $data['seller_id']])->count();
            $msg['data'] = $data;
            $msg['msg'] = '获取成功';
        }
        return json($msg);
    }

    public function me_evaluate_list()
    {
        $uid = token_decode(req('token'));
        $star = req('star');
        $page = req('page') ? req('page') : 1;
        $page_size = req('page_size') ? req('page_size') : 10;
        $where['seller_id'] = $uid;
        $where['type'] = 'seller';
        if(!empty($star)){
            $where['star'] = $star;
        }
        $data = Db::table('evaluate')->field('order_id,uid,star,text,image1,image2,time')->where($where)->where('time>complete_time or complete_time<=' . time() - 30000)->page($page, $page_size)->select();
        foreach ($data as $index => $datum) {
            unset($data[$index]['uid']);
            $user = Db::table('user')->field('headimg,name')->where('id', $datum['uid'])->find();
            $data[$index] = array_merge($data[$index], $user);
        }
        return json(['state' => 1, 'data' => $data, 'msg' => '获取成功']);
    }

}